﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class 分页 : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { 
        
         GridBind();
        }
       
        
    }

    protected void GridBind()
    { //DataGrid数据邦定 构造函数

        DataSet ds = new DataSet();
         SqlConnection MyConn = new SqlConnection(ConfigurationManager.AppSettings["SqlConn"]);
         SqlDataAdapter adapter = new SqlDataAdapter("Select * from zhuanti", MyConn);

        adapter.Fill(ds, "newsid");

        MyDataGrid.DataSource = ds.Tables["newsid"].DefaultView;
        Response.Write(ds.Tables["newsid"].Columns.Count);
        MyDataGrid.DataBind();
    }

    private void ShowStats()
    {
        //显示当前页面是第几页
        lblCurrentIndex.Text = "第 " + (MyDataGrid.CurrentPageIndex + 1).ToString() + " 頁";
        //显示总页数
        lblPageCount.Text = "總共 " + MyDataGrid.PageCount.ToString() + " 頁";
    }
    protected void PageButtonClick(object sender, EventArgs e)
    {

        //取得按钮单击时传递的命令参数
        string arg = ((LinkButton)sender).CommandArgument.ToString();
        switch (arg)
        {
            //如果点击的是“下一页”
            case "next":
                //如果当前页不是最后一页
                if (MyDataGrid.CurrentPageIndex < (MyDataGrid.PageCount - 1))
                {
                    //设置DataGrid控件的当前页索引为下一页面
                    MyDataGrid.CurrentPageIndex += 1;
                }
                break;
            //如果点击的是“前一页”
            case "prev":
                //如果当前页不是首页
                if (MyDataGrid.CurrentPageIndex > 0)
                {
                    //设置DataGrid控件的当前页索引为上一页面
                    MyDataGrid.CurrentPageIndex -= 1;
                }
                break;
            //如果点击的是“最后页”
            case "last":
                //设置当前页的索引为最后一页
                MyDataGrid.CurrentPageIndex = (MyDataGrid.PageCount - 1);
                break;
            //默认为”最首页“
            default:
                //设置当前页的索引为首页
                MyDataGrid.CurrentPageIndex = System.Convert.ToInt32(arg);
                break;
        }
        ShowStats();
        GridBind();

       // Response.Write(arg);
       // Response.Write("<br>" + MyDataGrid.CurrentPageIndex);
    }
    public void editDataGrid_Page(object sender, DataGridPageChangedEventArgs e)
    {
        //设置DataGrid当前页的索引值为用户选择的页的索引
        MyDataGrid.CurrentPageIndex = e.NewPageIndex;
        //重新绑定数据
        GridBind();
        //显示当前分页信息
        ShowStats();
    }
}

